package com.zwj.interview.双指针;

/**
 * @Author: Zwj
 * @Date: 2022/9/19 21:49
 */
public class 两个单词的最小距离 {

    public int shortestDistance(String[] words, String word1, String word2) {
        int a = -1;
        int b = -1;
        int result = Integer.MAX_VALUE;
        //我们的目的就是移动a,b指针，无所谓a,b的顺序
        for (int i = 0; i < words.length; i++) {
            if (word1.equals(words[i])) {
                a = i;
            } else if (word2.equals(words[i])) {
                b = i;
            }
            if (a != -1 && b != -1) {
                result = Math.min(result, Math.abs(a - b));
            }

        }
        return result;
    }


}